/**
 * 添加/修改 数据相关窗口
 */
$(document).ready(function() {
	/**
	 * 插入可编辑行
	 */
	var appendNewRow = function(gridName){
		$("#" + gridName).datagrid("appendRow",{
			fieldName: "",
			fieldValue: "",
			fieldType: "StringField"
		});
		var rowIndex = $("#" + gridName).datagrid("getRows").length - 1;
		$("#" + gridName).datagrid("beginEdit",rowIndex);
	}
	
	/**
	 * 删除一个字段行
	 */
	var deleteOneRow = function(gridName){
		var selectRows = $("#" + gridName).datagrid("getSelections");
		if(selectRows.length == 0){
			$.messager.alert("警告","请选择要删除的行！");
		}else{
			
			var index = 0
			for(var i=0;i<selectRows.length;i++){
				var oneRow = selectRows[i];
				var rowIndex = $("#" + gridName).datagrid("getRowIndex",oneRow);
				$("#" + gridName).datagrid("deleteRow",rowIndex);
			}
		}
	}
	
	/**
	 * 初始化编辑表格
	 */
	var initInsertGrid = function(gridName){
		$("#" + gridName).datagrid({
			iconCls:"icon-save",
			width: 550,
			height: "auto",
			striped: true,//斑马线
			rownumbers: true,
			singleSelect: false,//是否单选
			columns:[[
				{field : "checkbox",checkbox: true},
				{field: "fieldName",title:"字段名称",width:150,editor:"text"},
				{field: "fieldValue",title:"字段值",width:150,editor: "textarea"},
				{field: "fieldType",title:"字段类型",width:150,editor:{
					type: "combobox",
					options: {data : [{
							"value" : "StringField",
							"text" : "StringField"
						},{
							"value"  : "IntField",
							"text" : "IntField"
						},{
							"value" : "TextField",
							"text" : "TextField"
						},{
							"value" : "LongField",
							"text" : "LongField"
						}],
						editable: false,
						panelHeight: "auto" //设置容器高度自动增长
					}
				}}
			]],
			toolbar:[{
				text: "添加新字段",
				iconCls: "icon-add",
				handler: function(){
					appendNewRow(gridName);
				}
			},{
				text: "删除字段",
				iconCls: "icon-cancel",
				handler: function(){
					deleteOneRow(gridName);
				}
			}],
			onDblClickRow: function(rowIndex, rowData){//双击
				$("#" + gridName).datagrid("beginEdit",rowIndex);
			},
			onAfterEdit: function(index,row){
				row.editing = false;
				$("#" + gridName).datagrid("refreshRow",index);
			},
			onCancelEdit: function(index,row){
				row.editing = false;
				$("#" + gridName).datagrid("refreshRow",index);
			}
		});
	}
	
	/**
	 * 新增窗口
	 */
	$("#insertWindow").window({
		title : "新增",
		collapsible: false,
		minimizable: false,
		closed: true,
		modal : true,
		top: 80,
		icon: "icon-save",
		onOpen: function(){
			initInsertGrid("insertTab");
			resetInsertGrid();
		}
	})
	
	/**
	 * 修改窗口
	 */
	$("#updateWindow").window({
		title : "修改",
		collapsible: false,
		minimizable: false,
		closed: true,
		modal : true,
		top: 80,
		icon: "icon-save",
		onOpen: function(){
			initInsertGrid("updateTab");
			resetUpdateGrid();
		}
	})
});

/**
 * 重置新增表格
 */
var resetInsertGrid = function(){
	$("#insertTab").datagrid("loadData",
		{"rows":[{
			fieldName: "contentid",
			fieldValue: "",
			fieldType: "StringField"
		}]
	});
	$("#insertTab").datagrid("beginEdit",0);
}

/**
 * 重置修改表格
 */
var resetUpdateGrid = function(){
	maskOnLoad("正在加载，请稍后。。。");
	//获得选中的 contentid
	var selectRow = $("#allData").datagrid("getSelected");
	
	var indexPath = getSelectPath();
	//得到该 contentid 的详细数据
	var contentid = selectRow.contentid;
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : "luceneAction_getDataById?indexPath=" + indexPath,
		data: {contentid: contentid},
		success : function(data){
			closeMask();
			$("#updateTab").datagrid("loadData",data);
		},
		error : function(XMLHttpRequest, textStatus,errorThrown){
			closeMask();
			$.messager.alert("失败","初始化数据失败！");
		}
		
	});
}

/**
 * 遍历grid 获得数据
 */
var getGridData = function(gridName){
	//docFieldList
	/*
	private String fieldName;//字段名称
	private String fieldValue;//字段值
	private String fieldType;//字段类型	StringField	IntField	TextField
	*/
	
	var saveData = {};
	
	var rows = $("#" + gridName).datagrid("getRows");
	
	for(var i = 0;i<rows.length;i++){
		$("#" + gridName).datagrid("endEdit",i);
		
		var oneRow = rows[i];
		
		var fieldName = oneRow.fieldName;
		var fieldValue = oneRow.fieldValue;
		var fieldType = oneRow.fieldType;
		
		if("" == fieldName || "" == fieldValue || "" == fieldType){
			$("#" + gridName).datagrid("beginEdit",i);
			$.messager.alert("警告","第" + (i + 1) + "行数据不能为空！");
			return null;
		}else{
			saveData["docFieldList["+i+"].fieldName"]=fieldName;
			saveData["docFieldList["+i+"].fieldValue"]=fieldValue;
			saveData["docFieldList["+i+"].fieldType"]=fieldType;
		}
	}
	
	console.log(saveData);
	return saveData;
}

/**
 * 新增或者修改
 */
var createOrUpdateDoc = function(type){
	var indexPath = getSelectPath();
	
	var urlStr = "luceneAction_";
	var gridName = "";
	var windowName = "";
	if(type == "create"){
		urlStr += "insertOneDocument";
		gridName = "insertTab";
		windowName = "insertWindow";
	}else{
		urlStr += "updateOneDocument";
		gridName = "updateTab";
		windowName = "updateWindow";
	}
	var jsonData = getGridData(gridName);
	
	if(jsonData != null){
		maskOnLoad("正在保存，请稍后。。。");
		
		$.ajax({
			type : "POST",
			dataType : "json",
			url : urlStr + "?indexPath=" + indexPath,
			data: jsonData,
			success : function(data){
				closeMask();
				if(data.success != null){
					$.messager.alert("成功",data.success);
					$("#" + windowName).window("close");
					$("#allData").datagrid("reload");
				}else{
					$.messager.alert("失败",data.error);
				}
			},
			error : function(XMLHttpRequest, textStatus,errorThrown){
				closeMask();
				$.messager.alert("警告","保存失败！");
			}
		});
	}
}